#define _CRT_SECURE_NO_WARNINGS 1

#include "string.h"

size_t MyStrlen1(const char* p)
{
	assert(p);
	int count = 0;
	while (*p != '\0')
	{
		count++;
		p++;
	}
	return count;
}

size_t MyStrlen2(const char* p)
{
	assert(p);
	if (*p == '\0')
		return 0;
	else
		return 1 + MyStrlen2(++p);
}

size_t MyStrlen3(const char* p)
{
	assert(p);
	const char* ret = p;
	while (*p != '\0')
	{
		p++;
	}
	return p - ret;
}

char* MyStrcpy(char* dest, const char* src)
{
	assert(dest && src);
	char* ret = dest;
	while (*dest++ = *src++)
	{
		;
	}
	return ret;
}

char* MyStrcat(char* dest, const char* src)
{
	assert(dest && src);
	char* ret = dest;
	while (*dest != '\0')
	{
		dest++;
	}
	while (*dest++ = *src++)
	{
		;
	}

	return ret;
}

int MyStrcmp(const char* str1, const char* str2)
{
	assert(str1 && str2);
	while (*str1 == *str2)
	{
		if (*str1 == '\0')
		{
			return 0;
		}
		str1++;
		str2++;
	}

	//if (*str1 > *str2)
	//	return 1;
	//else
	//	return -1;
	
	return *str1 - *str2;

}

char* MyStrncpy(char* dest, const char* src, int count)
{
	assert(dest && src);
	char* ret = dest;
	for (int i = 0; i < count; i++)
	{
		if (*src != '\0')
		{
			*dest++ = *src++;
		}
	}
	*dest++ = '\0';
	return ret;
}

char* MyStrncat(char* dest, const char* src, int count)
{
	assert(dest && src);
	char* ret = dest;
	while (*dest != '\0')
	{
		dest++;
	}
	for (int i = 0; i < count; i++)
	{
		if(*dest != '\0')
			*dest++ = *src++;
	}
	*dest = '\0';
	return ret;
}

int MyStrncmp(const char* str1, const char* str2, int count)
{
	assert(str1 && str2);
	for (int i = 0; i < count; i++)
	{
		if (*str1 > *str2)
		{
			return 1;
		}
		else if(*str1 < *str2)
		{
			return -1;
		}
		else
		{
			str1++;
			str2++;
		}
	}

	return 0;
}

const char* MyStrstr(const char* str1, const char* str2)
{
	assert(str1 && str2);
	const char* s1 = NULL;
	const char* s2 = NULL;
	const char* cur = str1;

	while (*cur)
	{
		s1 = cur;
		s2 = str2;
		while(*s1 != '\0' && *s2 != '\0' && * s1 == *s2)
		{
			s1++;
			s2++;
		}

		if (*s2 == '\0')
			return cur;

		if (*s1 == '\0')
			return NULL;

		cur++;
	}
	return NULL;
}

